--#include "data\config\misc\TheShenArmyConfig.lua" once	--配置
--#include "data\functions\Activity\Common\Consumes.lua" --消耗 通用
--#include "data\functions\Consume.lua" once




--升级处理处理
MiscsImplement.TheShenArmyData = function( sysarg, msgid, packet )
	local TheShenArmy = Actor.getIntProperty(sysarg, PROP_ACTOR_THE_SHEN_ARMY)
	local var = Actor.getStaticVar(sysarg)
	if not var.theShenArmy then
		var.theShenArmy = {}
	end
	
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sTheShenArmyData)
	if not netPack then return end
	local len  = #TheShenArmyCfg.TheShenArmyList
	DataPack.writeByte(netPack, len)
	for idx = 1, len do
		local  cfg = TheShenArmyCfg.TheShenArmyList[idx]
		DataPack.writeByte(netPack, idx)
		local state = System.getIntBit(TheShenArmy, idx - 1)
		DataPack.writeByte(netPack, state)
		DataPack.writeShort(netPack, var.theShenArmy[idx] or 0)
	end
	
	DataPack.flush(netPack)
end

MiscsImplement.TheShenArmyCollect = function( sysarg, msgid, packet )
	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if TheShenArmyCfg.activeLevel and not Actor.checkActorLevel(sysarg, TheShenArmyCfg.activeLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.TheShenArmy003, TheShenArmyCfg.activeLevel), ttFlyTip)
		return 
	end	
	local Idx = DataPack.readByte(packet)

	local config = TheShenArmyCfg.TheShenArmyList[Idx]
	if not config then
		System.trace("Can not find TheShenArmyList config, Idx="..Idx)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy001,ttFlyTip)
		return
	end

	local TheShenArmy = Actor.getIntProperty(sysarg, PROP_ACTOR_THE_SHEN_ARMY)
	local var = Actor.getStaticVar(sysarg)
	if not var.theShenArmy then
		var.theShenArmy = {}
	end

	local state = System.getIntBit(TheShenArmy, Idx - 1)

	if state > 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy002,ttFlyTip)
		return
	end

	if Idx > 1 then
		local preState = System.getIntBit(TheShenArmy, Idx - 2)
		if preState <= 0 then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy004,ttFlyTip)
			return
		end
	end

	if not var.theShenArmy[Idx] then
		var.theShenArmy[Idx] = 0
	end

	if var.theShenArmy[Idx] >= config.picNum then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy005,ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, config.consumes, 1 ) then
		return 
	end

	if not DoConsumeCond( sysarg, config.consumes, 1, LogIds.TheShenArmyLogId, Lang.LogTips.TheShenArmy ) then
		return
	end
	
	var.theShenArmy[Idx] = var.theShenArmy[Idx] + 1

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sTheShenArmyCollect)
	DataPack.writeByte(netPack, Idx)
	DataPack.writeShort(netPack, var.theShenArmy[Idx])	
	DataPack.flush(netPack)
end

MiscsImplement.TheShenArmyActive = function( sysarg, msgid, packet )
	--local level = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	if TheShenArmyCfg.activeLevel and not Actor.checkActorLevel(sysarg, TheShenArmyCfg.activeLevel, 0) then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.TheShenArmy003, TheShenArmyCfg.activeLevel), ttFlyTip)
		return 
	end	
	local Idx = DataPack.readByte(packet)

	local config = TheShenArmyCfg.TheShenArmyList[Idx]
	if not config then
		System.trace("Can not find TheShenArmyList config, Idx="..Idx)
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy001,ttFlyTip)
		return
	end

	local TheShenArmy = Actor.getIntProperty(sysarg, PROP_ACTOR_THE_SHEN_ARMY)
	local var = Actor.getStaticVar(sysarg)
	if not var.theShenArmy then
		var.theShenArmy = {}
	end

	local state = System.getIntBit(TheShenArmy, Idx - 1)

	if state > 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy002,ttFlyTip)
		return
	end

	if Idx > 1 then
		local preState = System.getIntBit(TheShenArmy, Idx - 2)
		if preState <= 0 then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy004,ttFlyTip)
			return
		end
	end

	if not var.theShenArmy[Idx] then
		var.theShenArmy[Idx] = 0
	end

	if var.theShenArmy[Idx] < config.picNum then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TheShenArmy006,ttFlyTip)
		return
	end

	TheShenArmy =  System.setIntBit(TheShenArmy, Idx - 1, true)


	Actor.setActorTheShenArmy(sysarg, TheShenArmy)

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sTheShenArmyActive)
	DataPack.writeByte(netPack, Idx)	
	DataPack.writeByte(netPack, 1)	
	DataPack.flush(netPack)
end



MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cTheShenArmyData, MiscsImplement.TheShenArmyData)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cTheShenArmyCollect, MiscsImplement.TheShenArmyCollect)
MiscsImplement.registerMsgHandler(LogicCmd.miscSys.sub.cTheShenArmyActive, MiscsImplement.TheShenArmyActive)